<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增商品 - 出售账号的')" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
    <th:block th:include="include :: summernote-css" />
    <style>
        .btn-outline-secondary{
            display: none;
        }
        .note-insert{
            display: none;
        }
        .modal-backdrop{position: relative}
    </style>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-account-add">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">游戏名称：</label>
                <div class="col-sm-8">
                    <select name="gameTypeId" id="gameTypeId" class="form-control m-b">
                        <option value="">--请选择游戏--</option>
                        <option th:each="game : ${gameList}" th:text="${game.gname}" th:value="${game.id}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">是否显示：</label>
                <div class="col-sm-8">
                    <select name="showStatus" class="form-control m-b" th:with="type=${@dict.getType('sys_show_hide')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">编号：</label>
                <div class="col-sm-8">
                    <input name="accountNo" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">帐号：</label>
                <div class="col-sm-8">
                    <input name="gameNo" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">标题：</label>
                <div class="col-sm-8">
                    <textarea name="title" class="form-control" required></textarea>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">价格：</label>
                <div class="col-sm-8">
                    <input name="price" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">详情：</label>
                <div class="col-sm-8">
                    <input type="hidden" id="detail-html" name="detail" value="">
                     <div class="ibox float-e-margins">
                        <div class="ibox-title">
                            <h5>详情介绍</h5>
                        </div>
                        <div class="ibox-content no-padding">
                            <div class="summernote"></div>
                        </div>
                     </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">图片上传：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="imgs" id="imgs" value=""/>
                    <input id="multipleFile" name="files" type="file" multiple>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">搜索条件：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="searchParam" id="search-param" class="form-control" value="" />
                    <input id="searchParam" class="form-control" value="" disabled />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">排序：</label>
                <div class="col-sm-8">
                    <input name="sortNum" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label"></label>
                <div class="col-sm-8">
                    <button type="button" class="btn btn-sm btn-primary" onclick="setcondition()">设置条件</button>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <input name="remark" class="form-control" type="text">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <th:block th:include="include :: summernote-js" />
    <script th:inline="javascript">
        $.fn.serializeObject = function () {
            var o = {};
            var a = this.serializeArray();
            $.each(a, function () {
                if (o[this.name]) {
                    if (!o[this.name].push) {
                        o[this.name] = [o[this.name]];
                    }
                    o[this.name].push(this.value || '');
                } else {
                    o[this.name] = this.value || '';
                }
            });
            return o;
        };
        var prefix = ctx + "system/account"
        $("#form-account-add").validate({
            rules:{
                gameTypeId:{
                    required: true
                }
            },
            focusCleanup: true
        });
        //富文本编辑
        $(document).ready(function () {
            $('.summernote').summernote({
                height: 300,
                lang: 'zh-CN',
                toolbar:[
                    ['style', ['style']],
                    ['font', ['bold', 'italic', 'underline', 'clear']],
                    ['fontname', ['fontname']],
                    ['color', ['color']],
                    ['para', ['ul', 'ol', 'paragraph']],
                    ['height', ['height']],
                    ['table', ['table']],
                    ['insert', ['link', 'picture', 'hr']],
                    ['view', ['fullscreen', 'codeview']],
                    ['help', ['help']]
                ],
                callbacks: {
                    onImageUpload: function (files, editor, $editable) {
                        sendFile(files);
                    }
                }
            });
        });

        function sendFile(files, editor, $editable) {
            var data = new FormData();
            data.append("file", files[0]);
            $.ajax({
                data : data,
                type : "POST",
                url : ctx + 'common/qiniu/upload',
                cache : false,
                contentType : false,
                processData : false,
                dataType : "json",
                success: function(res) {
                    $('.summernote').summernote('insertImage', res.url,res.fileName);
                },
                error:function(){
                    $.modal.alertError('上传失败')
                }
            });
        }

        // 多图上传
        $("#multipleFile").fileinput({
            uploadUrl: ctx + 'common/qiniu/uploads',
            uploadAsync: false
        }).on('filebatchuploadsuccess', function (event, data, previewId, index) {
            var rsp = data.response;
            $("#imgs").val(rsp.urls);
            log.info("return urls：" + rsp.urls)
            log.info("reutrn fileNames：" + rsp.fileNames)
        }).on('fileremoved', function (event, id, index) {
            $("input[name='" + event.currentTarget.id + "']").val('')
        })
        //设置条件
        function setcondition() {
           var typeid = $("#gameTypeId option:selected").val();
           var text = $("#gameTypeId option:selected").text();
           if(typeid == ''){
               $.modal.alertError('请选择游戏分类!');  return
           }
            var options = {
                title: text + '- 条件设置',
                width: '800',
                height: '600',
                url: prefix + "/setCodition/" + typeid,
                callBack: callback
            };
           $.modal.openOptions(options);
        }
        //设置条件回调
        function callback(index, layero) {
            var body = $.modal.getChildFrame(index);
            var results = [];
            body.find("input[type='radio']:checked").each(function(){
                var v = $(this).val();
                if(results.indexOf(v) == -1){
                    results.push($(this).val());
                }
            });
            body.find("input[type='checkbox']:checked").each(function(){
                var v = $(this).val();
                if(results.indexOf(v) == -1){
                    results.push($(this).val());
                }
            });
            //console.log(results)
            $("#searchParam").val(results.join(","));
            $("#search-param").val(results.join(","));
            $.modal.close(index);
        }
        function submitHandler() {
            if ($.validate.form()) {
                var shtml =  $('.summernote').summernote('code');
                console.log(shtml)
                if(shtml == '<p><br></p>' || shtml== '') {
                    $.modal.alertError('账号详情必填');  return
                }
                $('#detail-html').val(shtml);
                $.operate.save(prefix + "/add", $('#form-account-add').serializeObject());
            }
        }
    </script>
</body>
</html>